package com.zdkj.subway.stm.util;

import cn.hutool.core.collection.CollectionUtil;
import com.zdkj.subway.stm.dvo.SwStCheckDetailsDvo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class TvmCheckDetails {

    public static List<SwStCheckDetailsDvo> countCheckDetails(List<SwStCheckDetailsDvo> swStCheckDetailsList) {
        BigDecimal sumTotal = new BigDecimal("0.00");
		BigDecimal sumDiffTotal = new BigDecimal("0.00");

        BigDecimal sumCoinShowAmount = new BigDecimal("0.00");
        BigDecimal sumCoinRealAmount = new BigDecimal("0.00");
        BigDecimal sumCoinDiffAmount = new BigDecimal("0.00");
        BigDecimal sumCoinNonStandardAmount = new BigDecimal("0.00");

        BigDecimal sumCoinChangeShowAmount = new BigDecimal("0.00");
        BigDecimal sumCoinChangeRealAmount = new BigDecimal("0.00");
        BigDecimal sumCoinChangeDiffAmount = new BigDecimal("0.00");
        BigDecimal sumCoinChangeNonStandardAmount = new BigDecimal("0.00");

        BigDecimal sumPaperShowAmount = new BigDecimal("0.00");
        BigDecimal sumPaperRealAmount = new BigDecimal("0.00");
        BigDecimal sumPaperDiffAmount = new BigDecimal("0.00");
        BigDecimal sumPaperNonStandardAmount = new BigDecimal("0.00");

        BigDecimal sumPaperChangeShowAmount = new BigDecimal("0.00");
        BigDecimal sumPaperChangeRealAmount = new BigDecimal("0.00");
        BigDecimal sumPaperChangeDiffAmount = new BigDecimal("0.00");
        BigDecimal sumPaperChangeNonStandardAmount = new BigDecimal("0.00");

        BigDecimal sumWasteShowAmount = new BigDecimal("0.00");
        BigDecimal sumWasteRealAmount = new BigDecimal("0.00");
        BigDecimal sumWasteDiffAmount = new BigDecimal("0.00");
        BigDecimal sumWasteNonStandardAmount = new BigDecimal("0.00");

        if(CollectionUtil.isNotEmpty(swStCheckDetailsList)){
//            BigDecimal sumCoinShowAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinRealAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinRealAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinDiffAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinDiffAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinNonStandardAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinNonStandardAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//
//            sumCoinChangeShowAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinChangeRealAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinChangeDiffAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumCoinChangeNonStandardAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//
//            sumPaperShowAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperRealAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperDiffAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperNonStandardAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//
//            sumPaperChangeShowAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperChangeRealAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperChangeDiffAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumPaperChangeNonStandardAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getCoinChangeShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//
//            sumWasteShowAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getWasteShowAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumWasteRealAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getWasteRealAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumWasteDiffAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getWasteDiffAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
//            sumWasteNonStandardAmount = swStCheckDetailsList.stream() .map(SwStCheckDetailsDvo::getWasteNonStandardAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
            for (SwStCheckDetailsDvo x : swStCheckDetailsList) {

                if(x.getCoinBoxCode() != null){
                    if(x.getCoinShowAmount()!=null){
                        sumCoinShowAmount = sumCoinShowAmount.add(x.getCoinShowAmount());
                    }
                    if(x.getCoinRealAmount()!=null){
                        sumCoinRealAmount = sumCoinRealAmount.add(x.getCoinRealAmount());
                    }
                    if(x.getCoinDiffAmount()!=null){
                        sumCoinDiffAmount = sumCoinDiffAmount.add(x.getCoinDiffAmount());
                    }
                    if(x.getCoinNonStandardAmount()!=null){
                        sumCoinNonStandardAmount = sumCoinNonStandardAmount.add(x.getCoinNonStandardAmount());
                    }
                }

                if(x.getCoinChangeBoxCode() != null){
                    if(x.getCoinChangeShowAmount()!=null){
                        sumCoinChangeShowAmount = sumCoinChangeShowAmount.add(x.getCoinChangeShowAmount());
                    }
                    if(x.getCoinChangeRealAmount()!=null){
                        sumCoinChangeRealAmount = sumCoinChangeRealAmount.add(x.getCoinChangeRealAmount());
                    }
                    if(x.getCoinChangeDiffAmount()!=null){
                        sumCoinChangeDiffAmount = sumCoinChangeDiffAmount.add(x.getCoinChangeDiffAmount());
                    }
                    if(x.getCoinChangeNonStandardAmount()!=null){
                        sumCoinChangeNonStandardAmount = sumCoinChangeNonStandardAmount.add(x.getCoinChangeNonStandardAmount());
                    }
                }
                if(x.getPaperBoxCode() != null){
                    if(x.getPaperShowAmount()!=null){
                        sumPaperShowAmount = sumPaperShowAmount.add(x.getPaperShowAmount());
                    }
                    if(x.getPaperRealAmount()!=null){
                        sumPaperRealAmount = sumPaperRealAmount.add(x.getPaperRealAmount());
                    }
                    if(x.getPaperDiffAmount()!=null){
                        sumPaperDiffAmount = sumPaperDiffAmount.add(x.getPaperDiffAmount());
                    }
                    if(x.getPaperNonStandardAmount()!=null){
                        sumPaperNonStandardAmount = sumPaperNonStandardAmount.add(x.getPaperNonStandardAmount());
                    }
                }

                if(x.getPaperChangeBoxCode() != null){
                    if(x.getPaperChangeShowAmount()!=null){
                        sumPaperChangeShowAmount = sumPaperChangeShowAmount.add(x.getPaperChangeShowAmount());
                    }
                    if(x.getPaperChangeRealAmount()!=null){
                        sumPaperChangeRealAmount = sumPaperChangeRealAmount.add(x.getPaperChangeRealAmount());
                    }
                    if(x.getPaperChangeDiffAmount()!=null){
                        sumPaperChangeDiffAmount = sumPaperChangeDiffAmount.add(x.getPaperChangeDiffAmount());
                    }
                    if(x.getPaperChangeNonStandardAmount()!=null){
                        sumPaperChangeNonStandardAmount = sumPaperChangeNonStandardAmount.add(x.getPaperChangeNonStandardAmount());
                    }
                }

                if(x.getWasteBoxCode() != null){
                    if(x.getWasteShowAmount()!=null){
                        sumWasteShowAmount = sumWasteShowAmount.add(x.getWasteShowAmount());
                    }
                    if(x.getWasteRealAmount()!=null){
                        sumWasteRealAmount = sumWasteRealAmount.add(x.getWasteRealAmount());
                    }
                    if(x.getWasteDiffAmount()!=null){
                        sumWasteDiffAmount = sumWasteDiffAmount.add(x.getWasteDiffAmount());
                    }
                    if(x.getWasteNonStandardAmount()!=null){
                        sumWasteNonStandardAmount = sumWasteNonStandardAmount.add(x.getWasteNonStandardAmount());
                    }
                }
            }

        }
        //报表小计
        String cancelLine = "------";
        SwStCheckDetailsDvo subTotal = new SwStCheckDetailsDvo();
        subTotal.setTvmCode("合计");
        subTotal.setCoinBoxCode(cancelLine);
        subTotal.setCoinShowAmount(sumCoinShowAmount);
        subTotal.setCoinRealAmount(sumCoinRealAmount);
        subTotal.setCoinDiffAmount(sumCoinDiffAmount);
        subTotal.setCoinNonStandardAmount(sumCoinNonStandardAmount);

        subTotal.setCoinChangeBoxCode(cancelLine);
        subTotal.setCoinChangeShowAmount(sumCoinChangeShowAmount);
        subTotal.setCoinChangeRealAmount(sumCoinChangeRealAmount);
        subTotal.setCoinChangeDiffAmount(sumCoinChangeDiffAmount);
        subTotal.setCoinChangeNonStandardAmount(sumCoinChangeNonStandardAmount);

        subTotal.setPaperBoxCode(cancelLine);
        subTotal.setPaperShowAmount(sumPaperShowAmount);
        subTotal.setPaperRealAmount(sumPaperRealAmount);
        subTotal.setPaperDiffAmount(sumPaperDiffAmount);
        subTotal.setPaperNonStandardAmount(sumPaperNonStandardAmount);

        subTotal.setPaperChangeBoxCode(cancelLine);
        subTotal.setPaperChangeShowAmount(sumPaperChangeShowAmount);
        subTotal.setPaperChangeRealAmount(sumPaperChangeRealAmount);
        subTotal.setPaperChangeDiffAmount(sumPaperChangeDiffAmount);
        subTotal.setPaperChangeNonStandardAmount(sumPaperChangeNonStandardAmount);

        subTotal.setWasteBoxCode(cancelLine);
        subTotal.setWasteShowAmount(sumWasteShowAmount);
        subTotal.setWasteRealAmount(sumWasteRealAmount);
        subTotal.setWasteDiffAmount(sumWasteDiffAmount);
        subTotal.setWasteNonStandardAmount(sumWasteNonStandardAmount);
        swStCheckDetailsList.add(subTotal);

        //报表清点金额合计
        SwStCheckDetailsDvo totalSum = new SwStCheckDetailsDvo();
        totalSum.setTvmCode("清点金额");
        sumTotal = sumTotal.add(sumCoinRealAmount).add(sumCoinChangeRealAmount).add(sumPaperRealAmount).add(sumPaperChangeRealAmount).add(sumWasteRealAmount);
        totalSum.setCoinShowAmount(sumTotal);
        swStCheckDetailsList.add(totalSum);

		SwStCheckDetailsDvo totalDiffSum = new SwStCheckDetailsDvo();
        totalDiffSum.setTvmCode("差额合计");
        sumDiffTotal = sumDiffTotal.add(sumCoinDiffAmount).add(sumCoinChangeDiffAmount).add(sumPaperDiffAmount).add(sumPaperChangeDiffAmount).add(sumWasteDiffAmount);
        totalDiffSum.setCoinShowAmount(sumDiffTotal);
        swStCheckDetailsList.add(totalDiffSum);
        return swStCheckDetailsList;
    }
}
